package _1_常用数据类型_关键字;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

/**
 * 优先使用 LocalDate, LocalDatetime
 * LocalDate 和 LocalDatetime 的 minus、 plus方法, 会生成一个新的对象
 * @author bpzj on 6/19/2018.
 */

public class DateAndTime {

  @Test
  public void 性能比较(){
    long l = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
      String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
    }
    long l1 = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
      String dt2 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
    }
    long l2 = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
      String dt = LocalDateTime.now().toLocalDate().toString();
    }
    long l3 = System.currentTimeMillis();
    System.out.println(l1 - l);
    System.out.println(l2 - l1);
    System.out.println(l3 - l2);
  }
  /**
   * 时间、日期 转为 String 输出
   */
  @Test
  public void datetimePattern() {
    // 使用 Date 类型， SimpleDateFormat 格式化
    String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
    System.out.println(date);
    // 使用 LocalDateTime类型, DateTimeFormatter格式化
    String dt2 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
    System.out.println(dt2);
    System.out.println(LocalDateTime.now().toString());
    String dt3 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
    System.out.println(dt3);

    System.out.println("─────────────────────────");
    String dt4 = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);
    String dt5 = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE);
    String dt6 = LocalDateTime.now().format(DateTimeFormatter.ISO_TIME);
    System.out.println("ISO_DATE_TIME: " + dt4 + "\nISO_DATE     : " + dt5 + "\nISO_Time     : " + dt6);
    // DateTimeFormatter.ISO_DATE == LocalDateTime.now().toLocalDate().toString();
    System.out.println("──────────────────────────────────────────────────");
    dt4 = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    dt5 = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE);
    dt6 = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME);
    System.out.println("ISO_LOCAL_DATE_TIME: " + dt4 + "\nISO_LOCAL_DATE     : " + dt5 + "\nISO_LOCAL_TIME     : " + dt6);
  }

  /**
   * LocalDateTime/LocalDate 和 Date 之间转换
   */
  @Test
  public void trans() {
    LocalDateTime localDateTime = LocalDateTime.now();
    // LocalDateTime 转 Date
    Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    Date date1 = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
    // Date 转 LocalDateTime
    LocalDateTime dateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  }

  /**
   * 获取昨天 00:00:00 的 时间
   */
  @Test
  public void calendar() {
    // 1 推荐使用 LocalDate
    LocalDateTime time1 = LocalDate.now().minusDays(1).atStartOfDay();
    System.out.println(time1);

    // 不推荐使用calendar
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.add(Calendar.DAY_OF_MONTH, -1);

    Date time = calendar.getTime();
    String yes = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(time);
    System.out.println(yes);
  }

  @Test
  public void aboutDuration() {
    LocalDate now = LocalDate.now();
    Period period = Period.ofYears(4);
    Period period2 = Period.ofDays(365*4);
    System.out.println(now.plus(period).toString());
    System.out.println(now.plus(period2).toString());
    // 下面这个会报错，因为 YEARS 是估算的时间段
    Duration.of(4, ChronoUnit.YEARS);
  }

  @Test
  public void addDay(){
    LocalDate date = LocalDate.parse("2024-01-31");
    date = date.plusDays(1);
    System.out.println(date);

  }

}
